# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     __init__.py.py
   Description :
   Author :        ZhangYiLin
   date：          2020/12/01 12:57
-------------------------------------------------
   Change Activity:
                   2020/12/01 12:57
-------------------------------------------------
"""
import os
from openpyxl import load_workbook
from config.ConfUi import DATA_Path
from common.NewLoguru import global_log


def get_row_num(sheet):
    """获取有效数据的最大行号"""
    return sheet.max_row


def get_cols_num(sheet):
    """获取有效数据的最大列号"""
    return sheet.max_column


def get_row_values(sheet, row_num):
    """获取某一行的数据"""
    max_cols_num = get_cols_num(sheet)
    row_values = []
    for colsNum in range(1, max_cols_num + 1):
        value = sheet.cell(row_num, colsNum).value
        if value is None:
            value = ''
        row_values.append(value)
    return tuple(row_values)


def get_column_values(sheet, column_num):
    """获取某一列的数据"""
    max_row_num = get_row_num(sheet)
    column_values = []
    for rowNum in range(2, max_row_num + 1):
        value = sheet.cell(rowNum, column_num).value
        if value is None:
            value = ''
        column_values.append(value)
    return tuple(column_values)


def get_value_of_cell(sheet, row_num, column_num):
    """获取某一个单元格的数据"""
    value = sheet.cell(row_num, column_num).value
    if value is None:
        value = ''
    return value


def get_all_values_of_sheet(sheet):
    """获取某一个sheet页的所有测试数据，返回一个元祖组成的列表"""
    max_row_num = get_row_num(sheet)
    column_num = get_cols_num(sheet)
    all_values = []
    for row in range(2, max_row_num + 1):
        row_values = []
        for column in range(1, column_num + 1):
            value = sheet.cell(row, column).value
            if value is None:
                value = ''
            row_values.append(value)
        all_values.append(tuple(row_values))
    return all_values


class ParseExcel(object):

    def __init__(self, module, page):
        data_path = os.path.join(DATA_Path, module, page + '.xlsx')
        global_log.info('测试数据：{}'.format(data_path))
        self.wk = load_workbook(data_path)
        self.excelFile = DATA_Path

    def get_sheet_by_name(self, sheet_name):
        """获取sheet对象"""
        sheet = self.wk[sheet_name]
        return sheet


if __name__ == '__main__':
    ex = ParseExcel('TestLogin', 'TestLogin')
    sheet = ex.get_sheet_by_name('TestLogin')
    print(get_all_values_of_sheet(sheet))
